<template>
	<el-container>
		<el-aside width="240px">
			<el-container>
				<el-main class="nopadding">
					<blCategoryTree model="Media" title="素材分类" ref="group" @node-click="groupClick" />
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="upload">上传素材</el-button>
					<div style="width: 15px;">&nbsp;</div>
					<el-date-picker v-model="search.dateRange" type="daterange" align="right" unlink-panels
						range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD">
					</el-date-picker>
					<div class="right-panel-search" style="padding-left: 15px;">
						<el-input v-model="search.keyword" placeholder="素材名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch">查询</el-button>
					</div>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">

					</div>
				</div>
			</el-header>
			<div>
				<el-card shadow="never">
					<sc-select-filter :data="selectFilter" :selected-values="selectedValues" :label-width="80"
						@on-change="stateChange"></sc-select-filter>
				</el-card>
			</div>
			<el-main class="nopadding">
				<taTableCard hideAdd rowHeight="75px" :cardSize="[6,6,8,12,24]" ref="table" :apiObj="apiObj"
					row-key="id">
					<template #default="{item}">
						<div
							style="display: flex;flex-direction: row; justify-content: space-between; position: relative;">
							<template v-if="item.type=='image'">
								<div>
									<div style="height: 35px;width: 35px;">
										<el-image style="max-width: 35px; max-height: 35px;" preview-teleported
											hide-on-click-modal :src="item.src+'?x-oss-process=image/resize,w_35&t='"
											:preview-src-list="[item.src]" show-progress fit="contain"
											:z-index="19900501" :alt="item.title" />
									</div>
								</div>
							</template>
							<template v-else-if="item.type=='video'">
								<div v-if="item.cover_image">
									<div style="height: 35px;width: 35px;">
										<el-image style="max-width: 35px; max-height: 35px;" preview-teleported
											hide-on-click-modal
											:src="item.cover_image+'?x-oss-process=image/resize,w_35&t='"
											:preview-src-list="[item.cover_image]" show-progress fit="contain"
											:z-index="19900501" :alt="item.title" />
									</div>
								</div>
								<div v-else>
									<div style="height: 35px;width: 35px;">
										<el-image style="max-width: 35px; max-height: 35px;" preview-teleported
											hide-on-click-modal
											:src="item.src+'?x-oss-process=video/snapshot,t_0,f_jpg'"
											:preview-src-list="[item.src+'?x-oss-process=video/snapshot,t_0,f_jpg']"
											show-progress fit="contain" :z-index="19900501" :alt="item.title" />
									</div>
								</div>
							</template>
							<div style="padding-left: 15px;text-align: right; flex:1; overflow: hidden;">
								<span
									style="font-size: 14px;display: inline-block; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{item.title}}</span>
								<p style="color: #606266;">{{item.created_at}}</p>
							</div>
							<el-dropdown class="media-dropdown" trigger="click">
								<el-button size="small" icon="ArrowDown" @click.stop="" />
								<template #dropdown>
									<el-dropdown-menu>
										<el-dropdown-item>
											<el-button size="small" type="success" icon="Link"
												@click="handleCopy(item.src)">
												链接
											</el-button>
										</el-dropdown-item>
										<el-dropdown-item>
											<el-button size="small" type="primary" icon="Edit"
												@click.stop="table_edit(item)">
												编辑
											</el-button>
										</el-dropdown-item>
										<el-dropdown-item>
											<el-button size="small" type="danger" icon="Delete"
												@click.stop="table_del(item)">
												删除
											</el-button>
										</el-dropdown-item>
									</el-dropdown-menu>
								</template>
							</el-dropdown>


						</div>
					</template>
				</taTableCard>

			</el-main>
		</el-container>
	</el-container>

	<uploadDialog v-if="dialog.upload" ref="uploadDialog" @success="handleSuccess" @closed="dialog.upload=false">
	</uploadDialog>

	<saveDialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false">
	</saveDialog>

	<taMediaSelect ref="tms"></taMediaSelect>

</template>

<script>
	import saveDialog from './save'
	import uploadDialog from './upload'
	import previewDialog from './save'
	import scSelectFilter from '@/components/scSelectFilter'

	export default {
		name: 'media.manage',
		components: {
			saveDialog,
			uploadDialog,
			scSelectFilter
		},
		data() {
			return {
				medieType: '全部',
				dialogVisible: true,
				selectFilter: [{
					title: "素材类型",
					key: "type",
					options: [{
							label: "全部",
							value: null
						},
						{
							label: "图片",
							value: "image",
							icon: "el-icon-picture-filled"
						},
						{
							label: "视频",
							value: "video",
							icon: "el-icon-video-camera-filled"
						},
						{
							label: "文档",
							value: "document",
							icon: "el-icon-document"
						}
					]
				}],
				selectedValues: {
					type: [null],
				},
				dialog: {
					save: false,
					upload: false
				},
				apiObj: this.$API.system.media.list,
				selection: [],
				search: {
					keyword: null,
					dateRange: null
				}
			}
		},
		mounted() {
			this.$refs.tms.open((items) => {
				console.error(items)
			})
		},
		methods: {
			handleQuanXuan() {

				this.$refs.table2.toggleAllSelection()

				console.error(this.$refs.table2.selectedRows)

			},
			async handleCopy(text) {
				try {
					await navigator.clipboard.writeText(text);
					this.$message.success("复制成功！");
				} catch (err) {
					this.$message.error("复制失败，请手动复制");
				}
			},
			stateChange(e) {
				var params = {
					selectFilter: e
				}
				this.$refs.table.upData(params)
			},
			// 上传素材
			upload() {
				this.dialog.upload = true;
				this.$nextTick(() => {
					const checkedNode = this.$refs.group.getCurrentNode();
					this.$refs.uploadDialog.open().setData({
						category_id: checkedNode.id || null
					});
				})
			},
			table_preview(row) {
				this.dialog.preview = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('preview').setData(row)
				})
			},
			//编辑
			table_edit(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row);
				})
			},
			//查看
			table_show(row) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row, index) {

				return this.$alert("开发中...", "提示", {
					type: 'error'
				});

				var reqData = {
					id: row.id
				}
				var res = await this.$API.system.media.remove.post(reqData);
				if (res.code == 0) {
					this.$refs.table.tableData.splice(index, 1);
					this.$message.success(res.msg)
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//批量删除
			async batch_del() {
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success("操作成功")
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection) {
				this.selection = selection;
			},
			//树点击事件
			groupClick(data) {
				var params = {
					category_id: data.id
				}
				this.$refs.table.upData(params)
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSuccess(data, mode) {
				if (mode == 'add') {
					this.$refs.table.refresh()
				} else if (mode == 'edit') {
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style scoped>
	.media-dropdown {
		position: absolute;
		right: -17px;
		bottom: -10px;
		display: none;
	}

	:deep(.el-card__body:hover) .media-dropdown {
		display: block;
	}

	:deep(.el-card:has(.mediaSelect)) {
		border: 2px solid #409eff;
	}
</style>
